package com.example.demo.common;


import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拦截器为接口HandlerInterceptor和preHandle接口方法
        //拿到session，如果得到说明已经登陆了，否则无
        HttpSession session=request.getSession(false);//如果没session则不执行这一步
        if(session!=null&&session.getAttribute(Constant.SESSION_USERINFO_KEY)!=null){
            //表明已经登陆了
            return  true;
        }
        response.setStatus(401);//response表示回应
        return false;
    }
}
